package ysq.xyg.host.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.host.model.dto.HostReportExportDto;
import ysq.xyg.host.model.po.HostDetail;
import ysq.xyg.host.model.po.HostItem;
import ysq.xyg.host.model.po.HostReport;
import ysq.xyg.host.model.vo.HostReportVo;
import ysq.xyg.host.service.HostDetailService;
import ysq.xyg.host.service.HostItemService;
import ysq.xyg.host.service.HostReportService;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.model.po.WebScoreRecord;
import ysq.xyg.web.service.WebMemberService;
import ysq.xyg.web.service.WebScoreRecordService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 房产-楼盘报备controller层
 *
 * @author congcong tao
 * @date 2019-12-11 14:59:48
 */
@RestController
public class HostReportController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(HostReportController.class);

    @Autowired
    private HostReportService hostReportService;
    @Autowired
    private HostItemService hostItemService;
    @Autowired
    private WebMemberService webMemberService;
    @Autowired
    private WebScoreRecordService webScoreRecordService;

    /**
     * 分页查询
     *
     * @param hostReportVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-12-11 14:59:48
     */
    @BaseLog(module = "房产", className = "楼盘报备",methods = "分页查询", businessType = BaseConstant.SYS_BUSINESS_TYPE_GET, operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostReport/page",method = RequestMethod.POST)
    public ReturnEntity page(HttpServletRequest request, HostReportVo hostReportVo, int pageSize, int pageNum){
        SysUser sysUser = acceptCurrentUser(request);
        hostReportVo.setAccountId(sysUser.getAccountId());
        ReturnEntity returnEntity = hostReportService.page(hostReportVo, pageSize, pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-12-11 14:59:48
     */
    @RequestMapping(value = "/hostReport/getEntity", method = RequestMethod.GET)
    public ModelAndView getEntity(String id) {
        ModelAndView view = new ModelAndView("/host/hostReportUpdateOrSave");
        if (StrUtil.isNotEmpty(id)) {
            HostReport hostReport = hostReportService.selectByPrimaryKey(id);
            view.addObject("hostReport", hostReport);
        }
        return view;
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-12-11 14:59:48
     */
    @BaseLog(module = "房产", className = "楼盘报备", methods = "删除", businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE, operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostReport/delete")
    public ReturnEntity delete(HttpServletRequest request, String id){
        return hostReportService.delete(id, acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param hostReportVo Vo对象
     * @author congcong tao
     * @date 2019-12-11 14:59:48
     */
    @BaseLog(module = "房产", className = "楼盘报备", methods = "添加或修改", businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE, operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostReport/addOrUpdate", method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request, HostReportVo hostReportVo){
        return hostReportService.addOrUpdate(hostReportVo, acceptCurrentUser(request));
    }

    @RequestMapping("/hostReport/export")
    public void export(HttpServletResponse response, HostReportVo hostReportVo){
        List<HostReportExportDto> orderList = hostReportService.selectExportEntity(hostReportVo);
        String nowDate = DateUtil.formatDateTime(new Date());
        // 导出操作
        EasyPoiUtil.exportExcel(orderList, nowDate + "楼盘报备展示", "楼盘报备", HostReportExportDto.class, nowDate + "楼盘报备展示.xls", response);
    }

    @RequestMapping(value = "/hostReport/updateReportState",method = RequestMethod.POST)
    public ReturnEntity updateReportState(String id, String reportState){
        HostReport hostReport = new HostReport();
        hostReport.setId(id);
        hostReport.setUpdateTime(new Date());
        // 1、报备成功，2、客户到访，3、认筹成功，4、无效客户
        hostReport.setReportState(reportState);
        // 设置时间
        if (StringUtils.equals(reportState, BaseConstant.HOST_REPORT_STATE_TWO)) {
            hostReport.setStateTwoTime(new Date());
        }
        if (StringUtils.equals(reportState, BaseConstant.HOST_REPORT_STATE_FOUR)) {
            hostReport.setStateFourTime(new Date());
        }
        int num = hostReportService.updateByPrimaryKeySelective(hostReport);
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    @RequestMapping(value = "/hostReport/update",method = RequestMethod.POST)
    public ReturnEntity update(HttpServletRequest request, String id, String reportState, String allMoney, String feeMoney){
        HostReport hostReport = hostReportService.selectByPrimaryKey(id);
        hostReport.setId(id);
        hostReport.setUpdateTime(new Date());
        // 认筹成功
        hostReport.setReportState(reportState);
        hostReport.setStateThreeTime(new Date());
        hostReport.setAllMoney(allMoney);
        hostReport.setFeeMoney(feeMoney);
        int num = hostReportService.updateByPrimaryKeySelective(hostReport);
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        HostItem hostItem = hostItemService.selectByPrimaryKey(hostReport.getItemId());
        WebScoreRecord webScoreRecord = new WebScoreRecord(acceptCurrentUser(request));
        webScoreRecord.setScore(feeMoney);
        webScoreRecord.setExtraId(hostReport.getId());
        webScoreRecord.setRecordType("1");
        webScoreRecord.setMemberId(hostReport.getMemberId());
        webScoreRecord.setDescripts("推荐[" + hostItem.getName() + "]，获取" + feeMoney + "元佣金");
        webScoreRecordService.insert(webScoreRecord);
        // 增加金额
        WebMember webMember = webMemberService.selectByPrimaryKey(hostReport.getMemberId());
        Double money = webMember.getMoney();
        if (money == null) {
            money = 0.0;
        }
        webMember.setMoney(money + Double.parseDouble(feeMoney));
        webMemberService.updateByPrimaryKey(webMember);
        return new ReturnEntity();
    }
}